﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[PublishEvent]
	@Message xml
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @DialogHandle uniqueidentifier
	DECLARE @Id bigint
	
	
    SELECT @DialogHandle = [DialogHandle]
		  ,@Id = [ServiceMetadataId]
    FROM [ServiceMetadata]
    WHERE [Source] = 'InitiatorService' AND [Target] = 'RootService'
    
    IF @DialogHandle IS NULL
    BEGIN
		BEGIN DIALOG @DialogHandle
		FROM SERVICE [InitiatorService]
		TO SERVICE 'RootService';
		
		UPDATE [ServiceMetadata]
		SET [DialogHandle] = @DialogHandle
		WHERE [ServiceMetadataId] = @Id
    END;
    
    SEND ON CONVERSATION @DialogHandle (@Message);
END

